package bb.lanxing.devices.sync;

import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.bryton.BrytonDevice;
import bb.lanxing.lib.devices.bryton.bbcp.BBDevice;
import bb.lanxing.lib.devices.bryton.bbcp.DeviceLogFileInfo;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceSyncListener;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sync.FitTrans;
import bb.lanxing.util.DateUtil;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class BrytonSyncManager extends FitSyncManager implements BrytonDevice.BrytonCallback {
    private BrytonDevice mBrytonDevice;

    @Override
    protected boolean isIgnoreCRC() {
        return true;
    }

    @Override
    protected boolean isIgnoreDecoderError() {
        return true;
    }

    @Override
    public /* bridge */ /* synthetic */ void abort() {
        super.abort();
    }

    @Override
    public /* bridge */ /* synthetic */ void abortAll() {
        super.abortAll();
    }

    @Override
    public /* bridge */ /* synthetic */ void delete(long j) {
        super.delete(j);
    }

    @Override
    public /* bridge */ /* synthetic */ void deleteAll() {
        super.deleteAll();
    }

    @Override
    public /* bridge */ /* synthetic */ DeviceFile getCurrentItem() {
        return super.getCurrentItem();
    }

    @Override
    public /* bridge */ /* synthetic */ DeviceFile getItem(long j) {
        return super.getItem(j);
    }

    @Override
    public /* bridge */ /* synthetic */ String getPath(long j) {
        return super.getPath(j);
    }

    @Override
    public /* bridge */ /* synthetic */ int getSyncState(long j) {
        return super.getSyncState(j);
    }

    @Override
    public /* bridge */ /* synthetic */ Object getTag() {
        return super.getTag();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean isSynchronised(long j) {
        return super.isSynchronised(j);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean isSynchronisingWithMultiFiles() {
        return super.isSynchronisingWithMultiFiles();
    }

    @Override
    public /* bridge */ /* synthetic */ void onError(FitTrans fitTrans, Throwable th) {
        super.onError(fitTrans, th);
    }

    @Override
    public /* bridge */ /* synthetic */ void onFitReceived(FitTrans fitTrans) {
        super.onFitReceived(fitTrans);
    }

    @Override
    public /* bridge */ /* synthetic */ void onMesg(FitTrans fitTrans, Mesg mesg) {
        super.onMesg(fitTrans, mesg);
    }

    @Override
    public /* bridge */ /* synthetic */ void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
        super.onSport(fitTrans, sessionMesg);
    }

    @Override
    public /* bridge */ /* synthetic */ void readFileList() {
        super.readFileList();
    }

    @Override
    public /* bridge */ /* synthetic */ void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        super.registerSyncListener(deviceSyncListener);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean resync(DeviceFile deviceFile) {
        return super.resync(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ void setCurrentFile(DeviceFile deviceFile) {
        super.setCurrentFile(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ void setTag(Object obj) {
        super.setTag(obj);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean shouldSyncWithServer() {
        return super.shouldSyncWithServer();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync() {
        return super.sync();
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(long j) {
        return super.sync(j);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(DeviceFile deviceFile) {
        return super.sync(deviceFile);
    }

    @Override
    public /* bridge */ /* synthetic */ boolean sync(String str) {
        return super.sync(str);
    }

    @Override
    public /* bridge */ /* synthetic */ void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        super.unregisterSyncListener(deviceSyncListener);
    }

    public BrytonSyncManager() {
        super(new DefaultFitProcessor(2), DeviceHelper.getWorkDir(12));
        BrytonDevice brytonDevice = (BrytonDevice) XZDeviceHelper.getDeviceManager().getDevice(1, 12);
        this.mBrytonDevice = brytonDevice;
        if (brytonDevice != null) {
            brytonDevice.setBrytonCallback(this);
        }
    }

    @Override
    boolean doSync(DeviceFile deviceFile) {
        if (BrytonDevice.isDeviceConnected()) {
            this.mBrytonDevice.readLogFile((int) deviceFile.getId(), (byte) 1);
            return true;
        }
        return false;
    }

    @Override
    void doReadFileList() {
        BrytonDevice brytonDevice = this.mBrytonDevice;
        if (brytonDevice != null) {
            brytonDevice.readLogFileList();
        }
    }

    @Override
    public void onDataRead(int i, int i2, byte[] bArr, AbstractMap<String, String> abstractMap) {
        DeviceFile currentItem = getCurrentItem();
        if (currentItem != null && i == 14) {
            boolean z = true;
            if (i2 == 0 || i2 == 1) {
                if (i2 != 0) {
                    z = false;
                }
                onReceivedFitData(bArr, z);
                return;
            }
            notifySyncStatus(currentItem.getId(), 4);
        }
    }

    @Override
    public void onLogFileListRead(int i, DeviceLogFileInfo[] deviceLogFileInfoArr) {
        if (i == 0) {
            List<DeviceFile> arrayList = new ArrayList<>();
            if (deviceLogFileInfoArr != null) {
                for (DeviceLogFileInfo deviceLogFileInfo : deviceLogFileInfoArr) {
                    FitDeviceFile fitDeviceFile = new FitDeviceFile();
                    fitDeviceFile.duration = deviceLogFileInfo.activityTime;
                    fitDeviceFile.id = deviceLogFileInfo.fileId;
                    fitDeviceFile.startTime = (deviceLogFileInfo.fileId * 1000) - TimeZone.getDefault().getRawOffset();
                    fitDeviceFile.distance = deviceLogFileInfo.activityDistance;
                    fitDeviceFile.name = DateUtil.format0.format(fitDeviceFile.startTime);
                    fitDeviceFile.size = deviceLogFileInfoArr.length;
                    fitDeviceFile.timestamp = deviceLogFileInfo.fileId * 1000;
                    if (fitDeviceFile.distance > 10) {
                        arrayList.add(fitDeviceFile);
                    }
                }
            }
            notifyReadFileList(arrayList);
            return;
        }
        notifyReadFileList(null);
    }

    @Override
    public void onProgressUpdate(int i) {
        DeviceFile currentItem = getCurrentItem();
        if (currentItem != null) {
            notifyProgressUpdate(currentItem.getId(), i);
        }
    }

    @Override
    public boolean isSynchronising() {
        return BBDevice.isConnected() && super.isSynchronising();
    }

    @Override
    public void release() {
        super.release();
        BrytonDevice brytonDevice = this.mBrytonDevice;
        if (brytonDevice != null) {
            brytonDevice.setBrytonCallback(null);
            this.mBrytonDevice.abort();
        }
    }
}
